<?php

namespace app\index\service;

use app\common\model\Knowledge;
use app\common\model\LawyerImpression;
use app\index\dao\KnowledgeDao;
use app\index\model\UM as UM;
use think\Collection;

/**
 * Class KnowledgeService
 * @package app\index\service
 */
class KnowledgeService
{
    // dao
    private KnowledgeDao $knowledgeDao;
    private LawyerImpressionService $impressionService;

    public function __construct(KnowledgeDao $knowledgeDao, LawyerImpressionService $impressionService)
    {
        $this->knowledgeDao = $knowledgeDao;
        $this->impressionService = $impressionService;
    }

    /**
     * 知识列表
     * @param UM|null $user
     * @param $categoryId
     * @param $isHot
     * @param int|null $page
     * @param int|null $page_size
     * @return Collection
     */
    public function index(?UM $user, $categoryId, $isHot, $kw, ?int $page = null, ?int $page_size = null): Collection
    {
        return $this->knowledgeDao->select($categoryId, $isHot, $kw, [], $page, $page_size);
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return Knowledge
     */
    public function read(?UM $user, int $id): ?Knowledge
    {
        $record = $this->knowledgeDao->find($id);

        if (!$record) {
            return null;
        }

        event('KnowledgeRead', ['knowledgeId' => $record->id]);

        return $record;
    }

}
